from django.db import models
import django.utils.timezone as timezone


# Create your models here.


# 人员管理
class People(models.Model):
    name = models.CharField('姓名', max_length=50, null=True)
    password = models.CharField("密码", max_length=20)
    phone = models.CharField('手机号', max_length=11, unique=True)
    peopleState = models.BooleanField('员工状态', null=True)
    token = models.CharField("token", max_length=500, null=True)
    end_token = models.IntegerField("token过期时间", null=True)
    entryTime = models.DateField('入职时间', max_length=50, null=True, auto_now=True)
    role_auth = models.TextField("角色所有的权限",max_length=5000, null=True)  # 所拥有的项目和权限
    people = models.ForeignKey(to="Position", on_delete=models.CASCADE)  # 和人员外键

    class Meta:
        db_table = 'People'


# 职位表
class Position(models.Model):
    jobName = models.CharField('职位名称', max_length=64)
    job = models.CharField('对应职位', max_length=64, null=True)
    rank = models.CharField('对应职级', max_length=64, null=True)
    peopleNum = models.IntegerField('编制人数', null=True)

    class Meta:
        db_table = 'Position'


# 模板
class Templates(models.Model):
    name = models.CharField("模板名称", max_length=30)


# 整个项目
class Project(models.Model):
    name = models.CharField("项目名称", max_length=30, unique=True)  # 项目名称
    square = models.FloatField("总平方米", null=True)  # 总平方米
    createTime = models.DateField("项目开始日期", auto_now_add=True)
    predict_money = models.FloatField("预算、测算总金额", null=True)  # 预算、测算总金额
    predict_do = models.FloatField("预算、测算总工程量", null=True)  # 预算、测算总工程量
    total_money = models.FloatField("总计费用", null=True)  # 动态累计金额
    total_do = models.FloatField("总计工程量", null=True)  # 动态累计工程量汇总
    over_money = models.FloatField("剩余金额", null=True)  # 剩余金额
    over_do = models.FloatField("剩余工程量", null=True)  # 剩余工程量
    # 单方
    one_predict_money = models.FloatField("预算、测算总金额", null=True)  # 预算、测算总金额
    one_predict_do = models.FloatField("预算、测算总工程量", null=True)  # 预算、测算总工程量
    one_total_money = models.FloatField("总计费用", null=True)  # 动态累计金额
    one_total_do = models.FloatField("总计工程量", null=True)  # 动态累计工程量汇总
    one_over_money = models.FloatField("剩余金额", null=True)  # 剩余金额
    one_over_do = models.FloatField("剩余工程量", null=True)  # 剩余工程量
    templates = models.ForeignKey(to="Templates", on_delete=models.CASCADE, to_field="id")  # 关联模板
    modules = models.ManyToManyField(to='Modules')  # 权限

    class Meta:
        db_table = "Project"

    # 动态负责范围   几楼到多少楼


# 动态负责范围
class Place(models.Model):
    name = models.CharField("名称", max_length=30)  # 地方名称
    square = models.FloatField("平方米", null=True)  # 平方米
    total_money = models.FloatField("总计造价", null=True)  # 每楼总计造价
    one_total_money = models.FloatField("单方总计造价", null=True)  # 每楼单方总计造价（指标）
    template = models.ForeignKey(to=Templates, to_field="id", on_delete=models.CASCADE)  # 去关联模板

    class Meta:
        db_table = "Place"

    # 土建工程 # 甲分包工程单方指标 # 甲 供材工程总造价


# level一级最高级   二级次之   依次类推
#
# 字段
class Modules(models.Model):
    name = models.CharField('标签名称', max_length=64)
    level = models.IntegerField('分类等级')
    pid = models.IntegerField('父级id')
    predict_money = models.FloatField("预算、测算总金额", null=True)  # 预算、
    total_money = models.FloatField("总计费用", null=True)  # 动态累计金额
    total_do = models.FloatField("总计工程量", null=True)  # 动态累计工程量汇总
    over_money = models.FloatField("剩余金额", null=True)  # 剩余金额
    over_do = models.FloatField("剩余工程量", null=True)  # 剩余工程量
    # 单方指标
    one_predict_money = models.FloatField("预算、测算总金额", null=True)  #
    one_predict_do = models.FloatField("预算、测算总工程量", null=True)  # 预
    one_total_money = models.FloatField("总计费用", null=True)  # 动态累计金
    one_total_do = models.FloatField("总计工程量", null=True)  # 动态累计工程量
    one_over_money = models.FloatField("剩余金额", null=True)  # 剩余金额
    one_over_do = models.FloatField("剩余工程量", null=True)  # 剩余工程量
    template = models.ForeignKey(to="Templates", to_field="id", on_delete=models.CASCADE)  # 去关联模板
    predict_do = models.FloatField("预算、测算总工程量", null=True)  # 预算、测算工程量
    unit = models.CharField("单位", max_length=32, null=True, blank=True)
    is_materials = models.BooleanField(default=0)  # 0为不是材料
    status = models.BooleanField(default=0)  # 状态

    class Meta:
        db_table = "Modules"


# 每天
class Day(models.Model):
    time = models.DateField("当前时间", null=True)
    # project = models.ForeignKey(to=Project, on_delete=models.CASCADE)
    total_money = models.FloatField("总计造价", null=True)  # 每楼总计造价
    one_total_money = models.FloatField("单方总计造价", null=True)  # 每楼单方总计造价（指标）


# 数据
class Record(models.Model):
    money = models.FloatField("金额", null=True)
    num = models.FloatField("数量", null=True)
    place = models.ManyToManyField(to="Place")
    modules = models.ManyToManyField(to="Modules")
    project = models.ForeignKey(to="Project", on_delete=models.CASCADE)
    day = models.ForeignKey(to="Day", on_delete=models.CASCADE)


# 每天 不同区域 总造价  单方指标  备注
class Remarks(models.Model):
    local = models.CharField("施工范围主要内容", max_length=200, null=True)
    remark = models.CharField("施工范围主要内容", max_length=200, null=True)
    day = models.ForeignKey(to="Day", on_delete=models.CASCADE)
    modules = models.ForeignKey(to='Modules', on_delete=models.CASCADE)
    project = models.ForeignKey(to="Project", on_delete=models.CASCADE)


class Materials(models.Model):
    # 一对多，
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    modules = models.ForeignKey(Modules, on_delete=models.CASCADE)
    materials_num = models.IntegerField("入库总量")
    materials_price = models.IntegerField("总价格")
    create_time = models.DateTimeField('最近入库时间', auto_now=True)
    class Meta:
        db_table = 'Materials'


# 通过监听库存数据的增加，生成记录表
class MaterialsHistory(models.Model):
    change_num = models.IntegerField('入库数量')
    change_price = models.IntegerField('入库价格')
    materials = models.ForeignKey(to='Materials', on_delete=models.CASCADE)
    add_time = models.DateTimeField('保存日期',default=timezone.now, blank=True)
    project = models.ForeignKey(to="Project", null=True, blank=True, on_delete=models.CASCADE)
    class Meta:
        db_table = 'MaterialsHistory'
